package algorithm.recursionAndbacktrack.combination.p17;

import java.util.*;

/**
 * @Author Administrator
 * @Date 2023/12/21--0:17
 * @Description
 */
class Solution_v3 {
    public List<String> letterCombinations(String digits) {
        if(digits.length() == 0) {
            return Collections.emptyList();
        }
        Map<Character, Character[]> map = new HashMap<>();
        map.put('2', new Character[] {'a', 'b', 'c'});
        map.put('3', new Character[] {'d', 'e', 'f'});
        map.put('4', new Character[] {'g', 'h', 'i'});
        map.put('5', new Character[] {'j', 'k', 'l'});
        map.put('6', new Character[] {'m', 'n', 'o'});
        map.put('7', new Character[] {'p', 'q', 'r', 's'});
        map.put('8', new Character[] {'t', 'u', 'v'});
        map.put('9', new Character[] {'w', 'x', 'y', 'z'});

        Character[][] chArrs = new Character[digits.length()][];
        for(int i = 0; i < chArrs.length; i++) {
            chArrs[i] = map.get(digits.charAt(i));
        }

        List<String> combinations = new ArrayList<>();
        letterCombinations(chArrs, combinations, new StringBuilder(), 0);

        return combinations;
    }

    private void letterCombinations(Character[][] chArrs, List<String> combinations, StringBuilder combination, int index) {
        if(chArrs.length == combination.length()) {
            combinations.add(combination.toString());
            return;
        }

        for(int i = 0; i < chArrs[index].length; i++) {
            combination.append(chArrs[index][i]);
            letterCombinations(chArrs, combinations, combination, index + 1);
            combination.deleteCharAt(combination.length() - 1);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution_v3().letterCombinations("23"));
    }
}
